﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc.Html;
using MvcControls.Web.Infrastructure;
using System.Web.Mvc;
using System.Linq.Expressions;

namespace MvcControls.Web.Helpers
{
    public static class DateRangeHelper
    {
        public static MvcHtmlString DateRangeFor<T, TReturn>(
            this HtmlHelper<T> html,
            Expression<Func<T, TReturn>> expressionFrom,
            Expression<Func<T, TReturn>> expressionTo,
            int noOfMonths = 3)
        {
            var from = ExpressionHelper.GetExpressionText(expressionFrom);
            var to = ExpressionHelper.GetExpressionText(expressionTo);

            var fromValue = html.ViewData.Eval(from);
            var toValue = html.ViewData.Eval(to);

            return DateRange(html, from, to, noOfMonths, fromValue, toValue);
        }

        public static MvcHtmlString DateRange(this HtmlHelper html, 
            string from = "from",
            string to = "to", 
            int noOfMonths = 3,
            object fromValue = null,
            object toValue = null)
        {
            return html.Partial(
                PathReader.GetHelperPathFor("DateRange.cshtml"),
                new DateRangeHelperInfo { 
                    FromFieldName = from, 
                    ToFieldName = to, 
                    FromValue = fromValue, 
                    ToValue = toValue,
                    NumberOfMonths = noOfMonths
                });
        }
    }
}